package com.pango.core.task;

import java.util.List;

import org.quartz.Scheduler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;

import com.pango.system.services.TimeTaskService;

/**
 * 继承ScheduleFactoryBean,在这边启动程序启动时候需要的定时器
 * @author thierry.fu
 *
 */
public class MySchedulerFactoryBean extends SchedulerFactoryBean{
	
	@Autowired
	private TimeTaskService timeTaskService;
	
	@Override
	public void afterPropertiesSet() throws Exception {
		super.afterPropertiesSet();
		/**
		 * 查询数据库中配置的所有Task
		 * 获取所有定义的job,判断不需要自启动的job，在启动的时候停止
		 */
		List<String> effectTaskNames =  timeTaskService.selectAllTaskNames();
		Scheduler scheduler  = this.getScheduler();
		String[] registedTriggers = scheduler.getTriggerNames(Scheduler.DEFAULT_GROUP);
		for (String trigger : registedTriggers) {
			if(effectTaskNames.contains(trigger)) {
				scheduler.pauseTrigger(trigger, Scheduler.DEFAULT_GROUP);
			}
		}
	}
}
